// 动态规划
// 需要维护当前的最小值，如果当前元素是负值，交换最小值和最大值的值再相乘

var maxProduct = function (nums) {
  let max = -Infinity,
      imax = 1,
      imin = 1;
  for (let i = 0; i < nums.length; i++) {
    if (nums[i] < 0) {
      let temp = imax;
      imax = imin;
      imin = temp;
    }
    imax = Math.max(nums[i], nums[i] * imax);
    imin = Math.min(nums[i], nums[i] * imin);
    max = Math.max(max, imax);
  }
  return max;
} 